home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CPPTASK.ARJ / CONOUT.CPP next >
C/C++ Source or Header  |  1991-08-01  |  3KB  |  116 lines

  1. /*
  2.    Sample module for channeling console output through
  3.    a single task to avoid problems with non-reentrant output
  4.    routines.
  5. */
  6.  
  7. #include "task.hpp"
  8. #include <conio.h>
  9. #include <string.h>
  10.  
  11. #define BUFSIZE   1024     /* Size of the console out buffer */
  12. #define STRLEN    256      /* Max. length of single output string */
  13. #define STACKSIZE 2048     /* Size of stack for output task */
  14.  
  15. local void far conout (void);
  16. local char conout_stack [STACKSIZE];
  17. local char conout_bufbuf [BUFSIZE];
  18. local char conout_str [STRLEN+1];
  19.  
  20. local buffer conout_buf(conout_bufbuf, BUFSIZE);
  21. local task conout_task((funcptr) conout, (byteptr) conout_stack,
  22.                        STACKSIZE, PRI_STD + 100, NULL);
  23.  
  24. /* -------------------------------------------------------------- */
  25.  
  26. /*
  27.    conout: The console output task. Reads strings from the buffer
  28.            and displays them on the console.
  29. */
  30.  
  31. local void far conout (void)
  32. {
  33.    int siz, i;
  34.    register int ch;
  35.  
  36.    while (1)
  37.       {
  38.       siz = conout_buf.read_buffer (conout_str, STRLEN, 0L);
  39.       for (i = 0; i < siz; i++)
  40.          {
  41.          switch (ch = conout_str [i])
  42.             {
  43.             case '\n':  putch ('\r');
  44.                         putch ('\n');
  45.                         break;
  46.             case 0x07:  sound (2000);
  47.                         t_delay (3L);
  48.                         nosound ();
  49.                         break;
  50.             default:    putch (ch);
  51.             }
  52.          }
  53.       }
  54. }
  55.  
  56.  
  57. /*
  58.    init_conout:   Creates buffer and task. Must be called
  59.                   before using any other routine from this module.
  60. */
  61.  
  62. void init_conout (void)
  63. {
  64.    conout_task.start_task ();
  65. }
  66.  
  67.  
  68. /*
  69.    end_conout: Deletes task and buffer. Should be called before
  70.                terminating CTask.
  71. */
  72.  
  73. void end_conout (void)
  74. {
  75.    conout_task.task::~task();
  76.    conout_buf.buffer::~buffer;
  77. }
  78.  
  79. /* -------------------------------------------------------------- */
  80.  
  81. /*
  82.    tprintf:    Buffered replacement for printf/cprintf.
  83. */
  84.  
  85. int tprintf (char *format, char *argptr)
  86. {
  87.    char buf [256];
  88.    int res;
  89.  
  90.    if ((res = vsprintf (buf, format, &argptr)) > 0)
  91.       if (conout_buf.write_buffer (buf, res, 0L) < 0)
  92.          res = 0;
  93.    return res;
  94. }
  95.  
  96.  
  97. /*
  98.    tputs:      Buffered replacement for puts.
  99. */
  100.  
  101. int tputs (char *buf)
  102. {
  103.    return (conout_buf.write_buffer (buf, strlen (buf), 0L) < 0) ? -1 : 0;
  104. }
  105.  
  106.  
  107. /*
  108.    tputch:     Buffered replacement for putch.
  109. */
  110.  
  111. int tputch (int ch)
  112. {
  113.    return (conout_buf.write_buffer (&ch, 1, 0L) < 0) ? EOF : ch;
  114. }
  115.  
  116.